package com.hutou.chuangke.widget.banner;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;


import com.hutou.chuangke.Bean.AdverBean;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.List;

public class BannerPagerAdapter
        extends RecyclingPagerAdapter {

    private Context mContext;
    private List<AdverBean> advers;

    private OnBannerClickListener mOnBannerClickListener;

    public BannerPagerAdapter(Context context, List<AdverBean> advers) {
        this.mContext = context;
        this.advers = advers;

        init();
    }

    public void setList(List<AdverBean> advers){
        this.advers = advers;
    }

    private void init() {
    }

    @Override
    public int getCount() {
        if (advers.size() == 0) {
            return 0;
        }
        return Integer.MAX_VALUE;
    }

    /**
     * get really position
     *
     * @param position
     * @return
     */
    private int getPosition(int position) {
        return position % advers.size();
    }

    @Override
    public View getView(int position, View view, ViewGroup container) {
        ViewHolder holder;
        if (view == null) {
            holder = new ViewHolder();
            view = holder.imageView = new ImageView(mContext);
            holder.imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            view.setTag(holder);
        } else {
            holder = (ViewHolder) view.getTag();
        }
        ImageLoader.getInstance().displayImage(advers.get(position % advers.size()).getImgurl(), holder.imageView);
        final int index = position;
        holder.imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mOnBannerClickListener != null) {
                    mOnBannerClickListener.onBannerClickListener(getPosition(index));
                }
            }
        });
        return view;
    }

    private static class ViewHolder {
        ImageView imageView;
    }

    public int getActualCount() {
        return advers.size();
    }

    public void setOnBannerClickListener(OnBannerClickListener onBannerClickListener) {
        mOnBannerClickListener = onBannerClickListener;
    }

    public interface OnBannerClickListener {
        public void onBannerClickListener(int position);
    }
}
